package yl.hs.bmipfull.service.laboratorydata.imple;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabTaskStartDetermineDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabTaskStartDetermineDetailsDao;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmFlyashInspectionItemEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabTaskStartDetermineDetailsEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabTaskStartDetermineEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabTaskStartDetermineDetailsService;
import yl.hs.bmipfull.service.laboratorydata.ILabTaskStartDetermineService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote 开盘鉴定信息服务层接口实现
 * @since 2022年07月25日 16时48分52秒 <br/>
 */
@Service
public class LabTaskStartDetermineServiceImpl extends LaboratorydataBaseService implements ILabTaskStartDetermineService {

    @Autowired
    ILabTaskStartDetermineDao dao;
    @Autowired
    ILabTaskStartDetermineDetailsDao detailsDao;

    @Override
    public List<LabTaskStartDetermineEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabTaskStartDetermineEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<LabTaskStartDetermineEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabTaskStartDetermineEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabTaskStartDetermineEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public LabTaskStartDetermineEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabTaskStartDetermine(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabTaskStartDetermineEntity entity = null;
        LabTaskStartDetermineDetailsEntity detailsEntity=null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new LabTaskStartDetermineEntity();
            detailsEntity= new LabTaskStartDetermineDetailsEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabTaskStartDetermineEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入开盘鉴定信息信息", Tl.toJson(entity), LabTaskStartDetermineEntity.class);
            }
            if (model.get("subData")!=null){
                var subDataList=(List<Map<String,Object>>)model.get("subData");
                for (var map:subDataList ) {
                    Tl.getChangeLog(detailsEntity, map);
                    detailsEntity.setDetermineSN(entity.getSn());
                    result = detailsDao.insertNew(detailsEntity);
                    if (result > 0) {
                        writeLog(ActionType.UPDATE, entity.getSn(), "插入开盘鉴定信息信息", Tl.toJson(detailsEntity), LabTaskStartDetermineDetailsEntity.class);
                    }
                }
            }


        } else {
            //执行修改
            detailsEntity= new LabTaskStartDetermineDetailsEntity();
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新开盘鉴定信息信息", Tl.toJson(entity), LabTaskStartDetermineEntity.class);
            }
            detailsDao.deleteByDetermineSN(entity.getSn());
            var subDataList=(List<Map<String,Object>>)model.get("subData");
            for (var map:subDataList ) {
                Tl.getChangeLog(detailsEntity, map);
                detailsEntity.setDetermineSN(entity.getSn());
                result = detailsDao.insertNew(detailsEntity);
                if (result > 0) {
                    writeLog(ActionType.UPDATE, entity.getSn(), "插入开盘鉴定信息信息", Tl.toJson(detailsEntity), LabTaskStartDetermineDetailsEntity.class);
                }
            }

        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabTaskStartDetermineEntity.class);
        }
        return result;
    }
}